
package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.entity.UserEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class BaseActivity extends Activity {

    /****************** 视图变量 **************************/
    private LinearLayout commonLeft;
    /****************** 全局变量 **************************/
    // 应用当前语言环境
    public static String commonLanguage = "1";
    // 是否登录
    public static boolean isLogin = false;
    // 当前城市商户最贵价格
    public static String maxPrice = "1000";
    // 用户信息
    public static UserEntity commonUserInfo = null;
    // 当前定位信息
    public static Location commonLocation = null;
    // 当前商区列表
    public static List<HashMap<String, Object>> areaList = null;
    // 当前选择城市名称
    public static String commonNowCityName = "重庆";
    // 当前定位城市名称
    public static String commonLocationCityName = "重庆";
    // 用于照相数据
    public static byte[] commonContent = null;

    protected Activity mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        mContext = this;
    }

    /**
     * 返回上一个界面
     */
    public void toBack() {
        this.commonLeft = (LinearLayout) findViewById(R.id.ll_common_left);
        this.commonLeft.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 寻找视图
     */
    public void findViews() {

    }

    /**
     * 设置监听
     */
    public void setListeners() {
        View.OnClickListener moreClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ll_common_left:
                        finish();
                        break;

                    default:
                        break;
                }
            }
        };
        this.commonLeft.setOnClickListener(moreClick);
    }

    public void showShortToast(Context context, String text) {
        showToast(context, text, Toast.LENGTH_SHORT);
    }

    public void showShortToast(String text) {
        showToast(this, text, Toast.LENGTH_SHORT);
    }

    public void showShortToast(Context context, String text, boolean hasTip) {
        showToast(context, "温馨提示:" + text, Toast.LENGTH_SHORT);
    }

    public void showLongToast(Context context, String text) {
        showToast(context, text, Toast.LENGTH_LONG);
    }

    public void showLongToast(String text) {
        showToast(this, text, Toast.LENGTH_LONG);
    }

    public void showLongToast(Context context, String text, boolean hasTip) {
        showToast(context, "温馨提示:" + text, Toast.LENGTH_LONG);
    }

    public void showToast(Context context, String text, int duration) {
        Toast.makeText(context, text, duration).show();
    }

    Dialog alertDialog = null;

    /**
     * 提示是否登录
     */
    public void showLoginDialog() {

        alertDialog = new AlertDialog.Builder(BaseActivity.this).setTitle("提示")
                .setMessage("你还未登录，是否登录！")
                .setIcon(android.R.drawable.ic_menu_help)
                .setPositiveButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                })
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent signedIntent = new Intent(BaseActivity.this,
                                LoginActivity.class);
                        startActivity(signedIntent);
                    }
                }).create();
        alertDialog.show();
    }

    private Dialog dialog;

    public void showListDialog(final List<HashMap<String, Object>> lists,
            final TextView resultTv) {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_list,
                null);
        dialog = new Dialog(BaseActivity.this, R.style.listDialog);
        ListView userlist = (ListView) view.findViewById(R.id.lv_dialog_list);
        SimpleAdapter adapter = new SimpleAdapter(this, lists,
                R.layout.dialog_list_item, new String[] {
                    "value"
                },
                new int[] {
                    R.id.tv_dialog_list_item_name
                });
        userlist.setAdapter(adapter);
        userlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                resultTv.setText(lists.get(position).get("value").toString());
                resultTv.setTag(lists.get(position).get("key"));
                dialog.dismiss();
            }

        }); // 当每一条目被点击后出发的事件
        dialog.setContentView(view); // 设置显示用户信息列表的对话框视图
        dialog.show();
    }

    public void showListDialog(final List<HashMap<String, Object>> lists,
            final ListDialogListener dialogListener) {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_list,
                null);
        dialog = new Dialog(BaseActivity.this, R.style.listDialog);
        ListView userlist = (ListView) view.findViewById(R.id.lv_dialog_list);
        SimpleAdapter adapter = new SimpleAdapter(this, lists,
                R.layout.dialog_list_item, new String[] {
                    "value"
                },
                new int[] {
                    R.id.tv_dialog_list_item_name
                });
        userlist.setAdapter(adapter);
        userlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                dialogListener.doSomething(position);
                dialog.dismiss();
            }

        }); // 当每一条目被点击后出发的事件
        dialog.setContentView(view); // 设置显示用户信息列表的对话框视图
        dialog.show();
    }

    public interface ListDialogListener {
        public void doSomething(int position);
    }

    private Dialog areaDialog;

    public void showAreaDialog(final List<HashMap<String, Object>> lists,
            final TextView resultTv) {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_list,
                null);
        areaDialog = new Dialog(BaseActivity.this, R.style.listDialog);
        ListView userlist = (ListView) view.findViewById(R.id.lv_dialog_list);
        SimpleAdapter adapter = new SimpleAdapter(this, lists,
                R.layout.dialog_list_item, new String[] {
                    "areaName"
                },
                new int[] {
                    R.id.tv_dialog_list_item_name
                });
        userlist.setAdapter(adapter);
        userlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                resultTv.setText(lists.get(position).get("areaName").toString());
                resultTv.setTag(lists.get(position).get("areaId"));
                areaDialog.dismiss();
            }

        }); // 当每一条目被点击后出发的事件
        areaDialog.setContentView(view); // 设置显示用户信息列表的对话框视图
        areaDialog.show();
    }

    public boolean commonIsNowCity() {
        if (!commonLocationCityName.contains(commonNowCityName)) {
            return false;
        }
        return true;
    }

    /**
     * 获取商区列表数据
     */
    public void commonRequestAreaListData() {

        AsyncLoadData ald = new AsyncLoadData(this, "", new AsyncAction() {

            @Override
            public void doLoad() throws Exception {
                // 获取提交参数
                Map<String, Object> addReviewParamMap = new HashMap<String, Object>();
                addReviewParamMap.put("cityName", commonNowCityName);
                // 发送请求
                byte[] bAreaList = HttpUtil.requestByPost(BaseActivity.this,
                        Constants.AREA_LIST_API, addReviewParamMap);
                if (bAreaList != null) {
                    areaList = new ParseJson().parseAreaListJson(bAreaList);
                }

            }

            @Override
            public void loadEnd() throws Exception {

            }

        });
        ald.execute(0);
    }
}
